package main

/*import (
	"bufio"
	"fmt"
	"io"
	"log"
	"os"
	"regexp"
	"strconv"
	"strings"
	"time"
	url2 "net/url"
)

type Reader interface {
	Read(rc chan []byte)
}
type Writer interface {
	Write(wc chan *Message)
}

type ReadFromFile struct {
	path string
}

type WriteToInfluxDB struct {
	influxDBDsn string
}

var i chan bool = make(chan bool);
//write
func (w *WriteToInfluxDB) Write(wc chan *Message) {
	for v:= range wc{
		fmt.Println(v)
	}

	i<-true
}

type LogProcess struct {
	rc chan []byte
	wc chan *Message

	read Reader
	write Writer
}

type Message struct {
	TimeLocal                 time.Time
	BytesSent                 int
	Path,Method,Scheme,Status string
	UpstreamTime, RequestTime float64
}


// read
func (r *ReadFromFile) Read(rc chan []byte) {

	f, err := os.Open(r.path)
	if err !=nil {
		panic(fmt.Sprintf("open file error:%s",err.Error()))
	}

	// 从文件尾部开始逐行读取文件内容
	f.Seek(0,2)
	rd := bufio.NewReader(f)

	for{
		line, err := rd.ReadBytes('\n')
		if err == io.EOF {
			time.Sleep(500 * time.Millisecond)
			continue
		}else if err != nil {
			panic(fmt.Sprintf("ReadBytes error:%s",err.Error()))
		}
		rc <- line[:len(line)-1]
	}

}


// process
func (l *LogProcess) Process() {
	// "-" |  "0.008" | "0.008" | 127.0.0.1 | - | [19/OCT/2018:23:30:57 +0800] | "GET /AUTH/LOGIN HTTP/1.1" | 200 | 3552 | "HTTP://NEW.MOYOKEJI.COM/"  | "MOZILLA/5.0 (MACINTOSH; INTEL MAC OS X 10_14_0) APPLEWEBKIT/537.36 (KHTML, LIKE GECKO) CHROME/69.0.3497.100 SAFARI/537.36"
	//'"$http_x_forwarded_for" |  "$request_time" | "$upstream_response_time" | $remote_addr | $remote_user | [$time_local] | "$request" | '
	//'$status | $body_bytes_sent | "$http_referer"  | "$http_user_agent" ';

	r := regexp.MustCompile(`\"([^"]+)\"\s+\|\s+\"([^"]+)\"\s+\|\s+([\d\.]+)\s+\|\s([^ \[]+)\s+\|\s+\[(.*)\]\s+\|\s+\"(.*?)\"\s+\|\s+([\d]+)\s+\|\s+([\d]+)\s+\|\s+\"(.*?)\"\s+\|\s+\"(.*?)\"`)


	loc,_:=time.LoadLocation("Asia/Shanghai")
	//loc, _ = time.LoadLocation("Local")
	log.Println(loc)
	// 循环读取内容
	for v:= range l.rc{

		ret := r.FindStringSubmatch(string(v))
		//log.Println(len(ret))
		//log.Println(ret)
		//log.Println("=========================================================")
		//log.Println(ret[0])
		//log.Println(ret[4])
		//log.Println("=========================================================")
		if len(ret) != 11 {
			log.Println("FindStringSubmatch fail:", string(v))
			continue
		}
		message :=&Message{}
		//log.Println(ret[5])
		t,err := time.ParseInLocation("02/Jan/2006:15:04:05 -0700", ret[5], loc)
		if err != nil{
			log.Println("ParseInLocation fail:",err.Error(),ret[5])
			continue
		}
		message.TimeLocal = t

		byteSent,_ := strconv.Atoi(ret[8])
		message.BytesSent = byteSent

		// GET /foo?query=t HTTP/1.0
		reqSli := strings.Split(ret[6]," ")

		if len(reqSli) != 3{
			log.Println("strings Split fail:",ret[6])
			continue
		}
		message.Method = reqSli[0]
		url,err := url2.Parse(reqSli[1])
		if err != nil{
			log.Println("url parse fail:",err.Error())
			continue
		}
		message.Path = url.Path

		// 协议：http
		//message.Scheme = ret[5]
		message.Scheme = "http"
		message.Status = ret[7]

		upstreamTime,_ := strconv.ParseFloat(ret[2],64)
		requestTime,_ := strconv.ParseFloat(ret[1],64)
		message.UpstreamTime = upstreamTime
		message.RequestTime = requestTime
		l.wc <- message


		//for _, v := range ret{
		//	fmt.Println(v)
		//}
		//l.wc <- ""
		//ret := r.FindStringSubmatch(string(v))
		//fmt.Println(ret)
		//l.wc<- strings.ToUpper(string(v))
	}


}


func main() {
	r := &ReadFromFile{
		path:"/var/log/nginx/access.log",
	}
	w := &WriteToInfluxDB{
		influxDBDsn:"username&password",
	}
	lp := &LogProcess{
		rc: make(chan []byte),
		wc: make(chan *Message),
		read:r,
		write:w,
	}
	for {
		go lp.read.Read(lp.rc)
		go lp.Process()
		go lp.write.Write(lp.wc)
		<-i
	}


}
*/
